package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import com.zretc.dbtools.RandomIdTools;
import com.zretc.device.dao.Device_InDao;
import com.zretc.device.entity.Device_In;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Device_InAddDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtDevCode;
	private JTextField txtInDate;
	private JTextField txtProvider;
	private JTextField txtProTele;
	private JTextField txtInNum;
	private JTextField txtPrice;
	private JTextField txtBuyer;
	private JTextField txtInId;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Device_InAddDialog dialog = new Device_InAddDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Device_InAddDialog() {
		setBounds(100, 100, 695, 592);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("入库添加");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(237, 13, 182, 47);
		contentPanel.add(label);

		JLabel label_8 = new JLabel("入库单号");
		label_8.setFont(new Font("宋体", Font.PLAIN, 25));
		label_8.setBounds(129, 67, 120, 30);
		contentPanel.add(label_8);

		txtInId = new JTextField();
		txtInId.setText(RandomIdTools.getRan());
		txtInId.setEditable(false);
		txtInId.setFont(new Font("宋体", Font.PLAIN, 25));
		txtInId.setColumns(10);
		txtInId.setBounds(263, 67, 250, 30);
		contentPanel.add(txtInId);

		txtDevCode = new JTextField();
		txtDevCode.setFont(new Font("宋体", Font.PLAIN, 25));
		txtDevCode.setColumns(10);
		txtDevCode.setBounds(263, 110, 250, 30);
		contentPanel.add(txtDevCode);

		JLabel label_1 = new JLabel("设备编号");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(129, 110, 120, 30);
		contentPanel.add(label_1);

		txtInDate = new JTextField();
		txtInDate.setFont(new Font("宋体", Font.PLAIN, 25));
		txtInDate.setColumns(10);
		txtInDate.setBounds(263, 156, 250, 30);
		contentPanel.add(txtInDate);

		JLabel label_2 = new JLabel("入库时间");
		label_2.setFont(new Font("宋体", Font.PLAIN, 25));
		label_2.setBounds(129, 156, 120, 30);
		contentPanel.add(label_2);

		txtProvider = new JTextField();
		txtProvider.setFont(new Font("宋体", Font.PLAIN, 25));
		txtProvider.setColumns(10);
		txtProvider.setBounds(263, 199, 250, 30);
		contentPanel.add(txtProvider);

		JLabel label_3 = new JLabel("供 应 商");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(129, 199, 120, 30);
		contentPanel.add(label_3);

		txtProTele = new JTextField();
		txtProTele.setFont(new Font("宋体", Font.PLAIN, 25));
		txtProTele.setColumns(10);
		txtProTele.setBounds(263, 242, 250, 30);
		contentPanel.add(txtProTele);

		JLabel label_4 = new JLabel("供应商电话");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(129, 242, 133, 30);
		contentPanel.add(label_4);

		txtInNum = new JTextField();
		txtInNum.setFont(new Font("宋体", Font.PLAIN, 25));
		txtInNum.setColumns(10);
		txtInNum.setBounds(263, 285, 250, 30);
		contentPanel.add(txtInNum);

		JLabel label_5 = new JLabel("入库数量");
		label_5.setFont(new Font("宋体", Font.PLAIN, 25));
		label_5.setBounds(129, 285, 106, 30);
		contentPanel.add(label_5);

		txtPrice = new JTextField();
		txtPrice.setFont(new Font("宋体", Font.PLAIN, 25));
		txtPrice.setColumns(10);
		txtPrice.setBounds(263, 328, 250, 30);
		contentPanel.add(txtPrice);

		JLabel label_6 = new JLabel("设备单价");
		label_6.setFont(new Font("宋体", Font.PLAIN, 25));
		label_6.setBounds(129, 328, 120, 30);
		contentPanel.add(label_6);

		txtBuyer = new JTextField();
		txtBuyer.setFont(new Font("宋体", Font.PLAIN, 25));
		txtBuyer.setColumns(10);
		txtBuyer.setBounds(263, 373, 250, 30);
		contentPanel.add(txtBuyer);

		JLabel label_7 = new JLabel("采 购 员");
		label_7.setFont(new Font("宋体", Font.PLAIN, 25));
		label_7.setBounds(129, 373, 120, 30);
		contentPanel.add(label_7);

		JButton button = new JButton("取消");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtInId.setText(RandomIdTools.getRan());
				txtDevCode.setText("");
				txtInDate.setText("");
				txtProvider.setText("");
				txtProTele.setText("");
				txtInNum.setText("");
				txtPrice.setText("");
				txtBuyer.setText("");
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(152, 429, 150, 38);
		contentPanel.add(button);

		JButton button_1 = new JButton("添加");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 获取文本框里的值
				String tinId = txtInId.getText();
				// 设备编号
				String device_code = txtDevCode.getText();
				// 入库日期
				String in_date = txtInDate.getText();
				// 供应商
				String provider = txtProvider.getText();
				// 供应商电话
				String tprovider_tele = txtProTele.getText();
				// 入库数量
				String tin_num = txtInNum.getText();
				// 单价
				String tprice = txtPrice.getText();
				// 采购者
				String buyer = txtBuyer.getText();

				// 判断输入的值
				if (device_code.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "编号不能为空");
				} else if (in_date.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "入库日期不能为空");
				} else if (provider.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "供应商不能为空");
				} else if (tprovider_tele.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "供应商电话不能为空");
				} else if (tprovider_tele.length() != 11) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "供应商电话长度输入错误");
				} else if (tprice.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "入库数量不能为空");
				} else if (tprice.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "设备单价不能为空");
				} else if (buyer.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "采购员姓名不能为空");
				} else {
					if (tprovider_tele.matches("^((13[0-9])|(15[^4])|(17[^9])|(18[^14]))\\d{8}$")) {
						// 转型
						int in_id = Integer.parseInt(tinId);
						// 入库数量
						int in_num = Integer.parseInt(tin_num);
						// 单价
						double price = Double.parseDouble(tprice);
						// 创建对象
						Device_InDao did = new Device_InDao();
						Device_In devin = new Device_In(in_id, device_code, in_date, provider, tprovider_tele, in_num,
								price, buyer);
						// 调用增加的方法
						boolean flag = did.addDevIn(devin);
						// 对返回值进行判断
						if (flag) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "添加成功");

						} else {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "添加失败,该单号已添加，请勿重复添加", "提示", JOptionPane.ERROR_MESSAGE);
						}

					} else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "供应商电话不是一个正确的号码");
					}

				}

			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(347, 429, 150, 38);
		contentPanel.add(button_1);

		// 窗口居中
		setLocationRelativeTo(null);
	}
}
